package src;
import java.util.*;
public class ScreeningRoom {
    private String name;
    private int rows;
    private int seatsPerRow;
    private boolean[][] seats;

    public ScreeningRoom(String name, int rows, int seatsPerRow) {
        this.name = name;
        this.rows = rows;
        this.seatsPerRow = seatsPerRow;
        this.seats = new boolean[rows][seatsPerRow];
        // 初始化所有座位为未预订
        for (int i = 0; i < rows; i++) {
            Arrays.fill(seats[i], false);
        }
    }

    public String getName() {
        return name;
    }

    public int getRows() {
        return rows;
    }

    public int getSeatsPerRow() {
        return seatsPerRow;
    }

    public boolean isSeatAvailable(int row, int seat) {
        if (row < 0 || row >= rows || seat < 0 || seat >= seatsPerRow) {
            return false;
        }
        return !seats[row][seat];
    }

    public boolean bookSeat(int row, int seat) {
        if (isSeatAvailable(row, seat)) {
            seats[row][seat] = true;
            return true;
        }
        return false;
    }

    public void cancelSeat(int row, int seat) {
        if (row >= 0 && row < rows && seat >= 0 && seat < seatsPerRow) {
            seats[row][seat] = false;
        }
    }

    public void displaySeats() {
        System.out.println("放映厅: " + name);
        System.out.print("  ");
        for (int i = 0; i < seatsPerRow; i++) {
            System.out.printf("%2d", i + 1);
        }
        System.out.println();

        for (int i = 0; i < rows; i++) {
            System.out.printf("%2d", i + 1);
            for (int j = 0; j < seatsPerRow; j++) {
                System.out.print(seats[i][j] ? " X" : " O");
            }
            System.out.println();
        }
    }
}    